
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:loading_more_list/loading_more_list.dart';
import 'package:qianyi/entity/home/trendVo.dart';
import 'package:qianyi/network/http_entity.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../entity/profile/my_user_info_entity.dart';
import '../../network/base_entity.dart';
import '../../network/http_config.dart';
import '../../network/http_manager.dart';
import '../../state/token.dart';
import '../../ui/widget/custom_refresh_mixin.dart';
import '../../utils/navigator_util.dart';


class TrendViewUserModel extends ChangeNotifier with CustomRefreshMixin {

  TabController? tabController;




  @override
  Future<List> requestList(int page) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    Map<String,dynamic> userInfo = jsonDecode(prefs.getString("userInfo")!);


    Map<String, dynamic> params = {};
    params["userId"] = userInfo["id"];
    params["type"] = "1";
    IBaseEntity? result = await HttpManager.getInstance().request(url: "/platform/user/getTrendByUser/${page}/10",method: Method.get, params: params);
    HttpEntity httpEntity = result as HttpEntity;
    TrendEntityList trendEntityList = TrendEntityList.fromJson2(httpEntity.oriData);
    List<String> imageIds = trendEntityList.getImageIds();
    Map<String,dynamic> params1 = {
      "imageIds": imageIds
    };

    HttpEntity httpEntity1 = await HttpManager.getInstance().request(url: "/platform/imgDetail/getCovers",method: Method.post,params: params1) as HttpEntity;
    List<String> returnImages = httpEntity1.getOriData()['data'].cast<String>();
    List<String> reversedImages = returnImages.reversed.toList();

    for (var i = 0; i < trendEntityList.trends!.length; i++) {
      trendEntityList.trends![i].cover = reversedImages[i];
    };
    notifyListeners();

    return trendEntityList.trends!;
  }

}
class TrendViewUserRepository extends LoadingMoreBase<TrendEntity> {
  TokenController tokenController = Get.find();

  TrendViewUserRepository({this.maxLength = 300});
  int _pageIndex = 1;
  bool _hasMore = true;
  bool forceRefresh = false;
  @override
  bool get hasMore => (_hasMore && length < maxLength) || forceRefresh;
  final int maxLength;
  Future<bool> refresh([bool notifyStateChanged = false]) async {
    _hasMore = true;
    _pageIndex = 1;
    //force to refresh list when you don't want clear list before request
    //for the case, if your list already has 20 items.
    forceRefresh = !notifyStateChanged;
    final bool result = await super.refresh(notifyStateChanged);
    forceRefresh = false;
    return result;
  }
  @override
  Future<bool> loadData([bool isloadMoreAction = false]) async {
    bool isSuccess = false;
    SharedPreferences prefs = await SharedPreferences.getInstance();
    Map<String,dynamic> userInfo = jsonDecode(prefs.getString("userInfo")!);


    Map<String, dynamic> params = {};
    params["userId"] = userInfo["id"];
    params["type"] = "1";
    IBaseEntity? result = await HttpManager.getInstance().request(url: "/platform/user/getTrendByUser/${_pageIndex}/10",method: Method.get, params: params);
    HttpEntity httpEntity = result as HttpEntity;
    TrendEntityList trendEntityList = TrendEntityList.fromJson2(httpEntity.oriData);
    List<String> imageIds = trendEntityList.getImageIds();
    Map<String,dynamic> params1 = {
      "imageIds": imageIds
    };

    HttpEntity httpEntity1 = await HttpManager.getInstance().request(url: "/platform/imgDetail/getCovers",method: Method.post,params: params1) as HttpEntity;
    List<String> returnImages = httpEntity1.getOriData()['data'].cast<String>();
    List<String> reversedImages = returnImages.reversed.toList();

    for (var i = 0; i < trendEntityList.trends!.length; i++) {
      trendEntityList.trends![i].cover = reversedImages[i];
    };
    _pageIndex++;
    addAll(trendEntityList!.trends!);

    isSuccess = true;
    return isSuccess;
  }

}
